<?php
namespace User\Model;
use Think\Model;

class UserModel extends Model {
    
    protected $trueTableName = 'users';
    protected $user = null;

    protected $token = '';
    protected $userId = 0;
    protected $userName = '';
    protected $userEmail = '';
    protected $userPhone = '';
    protected $userPassword = '';
    protected $salt = '';

    public function __construct() {
        parent::__construct();
        if (is_null($this->instance)) {
            $this->user = M('users');
        }
    }
    
    public function validateToken($uid, $token) {
        $token_ = $this->where("uid = '%d'", $uid)->getField('token');
        if ($token_ === $token) {
            return true;
        } else {
            return false;
        }
    }

    public function genToken($uid) {
        $newToken = sha1($uid.time());
        $this->user->where("uid = '%d'", $uid)->data(array('token' => $newToken))->save();
        return $newToken; 
    }

    public function register($userName, $userEmail, $userPhone, $userPassword) {
    
        $result = false;

        // get params
        $this->userName = $userName;
        $this->userEmail = $userEmail;
        $this->userPhone = $userPhone;
        $this->salt = $this->genSalt($this->userName);
        $this->userPassword = $userPassword;
        $this->hashPassword = $this->hashPassword($this->userName, $this->salt, $this->userPassword);

        $data['username'] = $this->userName;
        $data['email'] = $this->userEmail;
        $data['phone'] = $this->userPhone;
        $data['password'] = $this->hashPassword;
        $data['salt'] = $this->salt;
        $data['reg_date'] = null;
        $data['reg_ip'] = getClientIp();

        $fields = 'username,email,phone,password,salt,reg_date,reg_ip';
        
        if ($this->user->field($fields)->add($data)) {
            $result = true;
        } else {
            $resutl = false;
        }

        // register ok, add register log to database
        // TODO 
        if ($result) {

        }

        return $result;
    }

    public function login($username, $password) {
        
        if ($this->checkUserByName($username)) {
            $salt = $this->user->where("username = '%s'", $username)->getField('salt');
            $result = $this->user->where("username = '%s' and password = '%s'", $username, $this->hashPassword($username, $salt, $password))->find(); 
            if (is_array($result)) {
                // TODO
                // add the login event to log
                $data['last_login_ip'] = getClientIp();
                $data['last_login_data'] = null;
                $this->user->where("username = '%s'", $username)->data($data)->save();
                return $result;
            } else {
                return false;
            }
        } else {
            return null; 
        } 
    }

    protected function genSalt($username) {
    
        $unique = uniqid($username);
        return sha1($unique);
    }

    protected function hashPassword($username, $salt, $password) {
        
        $len = strlen($username);
        if ($len > 40) {
            $len = $len % 40;
        }
        $hashsalt = substr($salt, 0, $len);
        $hashpassword = sha1($password.$hashsalt);

        return $hashpassword;
    }

    protected function checkUserById($uid) {
    
        $result = $this->user->where("uid = '%d'", $uid)->find();
        return is_array($result) ? true : false;
    }

    public function checkUserByName($username) {
        
        $result = $this->user->where("username = '%s'", $username)->find();
        return is_array($result) ? true : false;
    }

    public function checkUserByEmail($email) {

        $result = $this->user->where("email = '%s'", $email)->find();
        return is_array($result) ? true : false;
    }

    public function checkUserByPhone($phone) {

        $result = $this->user->where("phone = '%s'", $phone)->find();
        return is_array($result) ? true : false;
    }

    public function validatePassword() {
        // TODO
        // need to abstract
    }

    public function getUserById($uid) {
        $result = $this->user->where("uid = '%d'", $uid)->field('uid,username,email,phone')->find();
        return $result;
    }

    public function getUserByName($username) {
        $result = $this->user->where("username = '%s'", $username)->field('uid,username,email,phone')->find();
        return $result;
    }

    public function getUserByEmail($email) {
        $result = $this->user->where("email = '%s'", $email)->field('uid,username,email,phone')->find();
        return $result;
    }

    public function getUserByPhone($phone) {
        $result = $this->user->where("phone = '%s'", $phone)->field('uid,username,email,phone')->find();
        return $result;
    }

    public function __destruct() {
        if (!is_null($this->user)) {
            $this->user = null;
        }
    }
}
